//
//  HRSearchUsersResultViewController.m
//  Hear
//
//  Created by mac on 2017/12/4.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSearchUsersResultViewController.h"
#import "HRHRSearchResultCell.h"
#import "MJRefresh.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>
#import "HRSearchUsersViewModel.h"
#import "HRDynamicViewController.h"
#import "HRUpdataUserInfoModel.h"

@interface HRSearchUsersResultViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *contentArray;
@property (nonatomic, assign)NSInteger pIndex;
@property(nonatomic, strong)HRSearchUsersViewModel *viewModel;

@end

@implementation HRSearchUsersResultViewController
#pragma mark - Get and Set
- (HRSearchUsersViewModel *)viewModel{
    if (_viewModel == nil) {
        _viewModel = [[HRSearchUsersViewModel alloc] init];
    }
    return _viewModel;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"搜索结果";
    
    self.contentArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.rowHeight = 70;
    if (@available(iOS 11.0, *))
    {
        self.tableView.estimatedRowHeight = 0;
    }
    if (self.Keywords.length){
        [self getUserInfo];
    }else{
        [self getContentData];
    }
}

- (void)getContentData{
    self.pIndex = 0;
    
    [self getContentData:YES];
    WeakSelf
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pIndex = 0;
        [self getContentData:YES];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    //    //上拉加载
    self.tableView.mj_footer=[MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.pIndex ++;
        [weakSelf getContentData:NO];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.contentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HRHRSearchResultCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HRHRSearchResultCell"];
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"HRHRSearchResultCell" bundle:nil] forCellReuseIdentifier:@"HRHRSearchResultCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"HRHRSearchResultCell"];
    }
    cell.infoModel = self.contentArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
    return NO;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"空白"];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = nil;
    UIFont *font = nil;
    UIColor *textColor = nil;
    text = @"还没此用户哦";
    font = CustomUIFont(16);
    textColor = MainColor;
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}






-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HRUpdataUserInfoModel *infoModel = self.contentArray[indexPath.row];
    HRDynamicViewController *VC = [HRDynamicViewController new];
    VC.currentUserID = infoModel.LocalId;
    [self.navigationController pushViewController:VC animated:YES];
}

- (void)getContentData:(BOOL)isHead{
    self.paramDict[@"PageSize"] = @"12";
    self.paramDict[@"PageIndex"] = [NSString stringWithFormat:@"%d",self.pIndex];
    self.paramDict[@"CurrentUserLocalId"] = USERID;
    
    WeakSelf
    [self.viewModel getSerachUser:self.paramDict contentArr:^(NSArray *contentArr) {
        if (isHead){
            [weakSelf.contentArray removeAllObjects];
        }
        [weakSelf.contentArray addObjectsFromArray:contentArr];
        [weakSelf.tableView reloadData];
        
        if (contentArr.count < 12){
            weakSelf.tableView.mj_footer.hidden = YES;
        }else{
            
            weakSelf.tableView.mj_footer.hidden = NO;
        }
    }];
}

- (void)getUserInfo{
    WeakSelf
    [self.viewModel getSearchResultUsersInfo:@{@"CurrentUserLocalId":USERID,@"Keywords":self.Keywords} isSeccess:^(HRUpdataUserInfoModel *userModel) {
        [weakSelf.contentArray addObject:userModel];
        [weakSelf.tableView reloadData];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
